<?php

class Admin_TipousuarioController extends App_Controller_Action
{
    
    private $_table     = 'TIPO_USUARIO';
    private $_pk        = 'TUS_ID';
    private $_order_col = 'TUS_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'TUS_STA';
    private $obj        = '';
    private $form       = '';
    public  $dir        = 'public/upload//';
    public  $_file_col  = 'TUS_FILE';
    
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Tipousuario();
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('elemform','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_list.js');
        
        $where = $this->_pk.' > 0';
        
        $stafiltro = $this->_getParam('stafiltro', '1'); 
        if($stafiltro != '') $where .= ' AND TUS_STA = ' . $stafiltro;
        $this->view->stafiltro  = $stafiltro;
        
        $itfiltro = $this->_getParam('itfiltro', ''); 
        if($itfiltro != '') $where .= ' AND TUS_NOM like "' . $itfiltro.'%"';
        $this->view->itfiltro = $itfiltro; 
        
        
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order.'';
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
        $this->view->cbs_id = $cbs_id;
        
    }
    
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_form_pdr.js'); 
        
       
        $this->view->form = $this->form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk);

                    $data = $this->form->getValues();
                    
                    if ($id == 0)
                        $this->db->insert($this->_table,$data);
                    else
                        $this->db->update($this->_table,$data, $this->_pk.' = ' . $id);

                    $this->_helper->redirector('index');      
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->form->populate($formData);
            }  
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk .' = ' . $id;
                $data = $this->db->fetchRow($query);
                $this->form->populate($data);
                
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $data = array($this->_sta_col => 0);
            $ok = (bool) $this->db->update($this->_table,$data, $this->_pk .' = ' . $id);

        }
        
        echo json_encode(array('ok' => $ok));
    }
    
}

